<template>
	<div class="app-container">
		<el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
			<el-form-item label="手机号" prop="phone">
				<el-input v-model="queryParams.phone" placeholder="请输入手机号" clearable />
			</el-form-item>
			<el-form-item label="类型" prop="specialFlag">
				<el-select v-model="queryParams.specialFlag" placeholder="请选择类型" clearable>
					<el-option label="呼入黑名单" value="2"></el-option>
					<el-option label="呼出黑名单" value="3"></el-option>
					<el-option label="呼入呼出黑名单" value="4"></el-option>
				</el-select>
			</el-form-item>
			<el-form-item>
				<el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
				<el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
			</el-form-item>
		</el-form>
		<el-row :gutter="10" class="mb8">
			<el-col :span="1.5">
				<el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
			</el-col>
		</el-row>
		<el-table v-loading="loading" :data="list" style="width: 100%;">
			<el-table-column label="序号" type="index" align="center">
				<template slot-scope="scope">
					<span>{{scope.$index + 1}}</span>
				</template>
			</el-table-column>
			<el-table-column label="手机号" align="center" prop="phone" :show-overflow-tooltip="true" />
			<el-table-column label="类型" align="center" prop="special_flag">
				<template slot-scope="scope">
					<span v-if="scope.row.special_flag == 2">呼入黑名单</span>
					<span v-if="scope.row.special_flag == 3">呼出黑名单</span>
					<span v-if="scope.row.special_flag == 4">呼入呼出黑名单</span>
				</template>
			</el-table-column>
			<el-table-column label="创建时间" align="center" prop="created_at" :show-overflow-tooltip="true" />
			<el-table-column label="操作" align="center" class-name="small-padding fixed-width">
				<template slot-scope="scope">
					<el-button type="text" icon="el-icon-edit" size="mini" @click="handleUpdate(scope.row)">修改</el-button>
					<el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>
		<!-- 添加或修改参数配置对话框 -->
		<el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
			<el-form ref="form" :model="form" label-width="100px">
				<el-form-item label="手机号" prop="nbr">
					<el-input v-if="title == '修改黑名单'" v-model="form.phone" placeholder="请输入手机号" disabled/>
					<el-input v-if="title == '新增黑名单'" v-model="form.phone" placeholder="请输入手机号" />
				</el-form-item>
				<el-form-item label="类型" prop="specialFlag">
					<el-select v-model="form.specialFlag" placeholder="请选择类型" style="width: 100%;">
						<el-option label="呼入黑名单" value="2"></el-option>
						<el-option label="呼出黑名单" value="3"></el-option>
						<el-option label="呼入呼出黑名单" value="4"></el-option>
					</el-select>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button type="primary" @click="submitForm">确 定</el-button>
				<el-button @click="cancel">取 消</el-button>
			</div>
		</el-dialog>
	</div>
</template>

<script>
	import { deleteSpecialPhone, getSpecialPhones, addSpecialPhone, updateSpecialPhone } from "@/api/call/blacklist";

	export default {
		name: "Online",
		data() {
			return {
				// 遮罩层
				loading: true,
				// 总条数
				total: 0,
				// 表格数据
				list: [],
				pageNum: 1,
				pageSize: 10,
				// 表单参数
				form: {},
				// 查询参数
				queryParams: {
					phone: undefined,
				},
				open: false,
				// 弹出层标题
				title: "",
			};
		},
		created() {
			this.getList();
		},
		methods: {
			/** 查询登录日志列表 */
			getList() {
				this.loading = true;
				getSpecialPhones(this.queryParams).then(response => {
					this.list = response.data;
					this.loading = false;
				});
			},
			submitForm() {
				this.loading = true;
				this.$refs["form"].validate(valid => {
					if(valid) {
						if(this.form.uuid != null) {
							updateSpecialPhone(this.form).then(response => {
								this.msgSuccess("修改成功");
								this.open = false;
								this.getList();
							});
						} else {
							addSpecialPhone(this.form).then(response => {
								this.msgSuccess("新增成功");
								this.open = false;
								this.getList();
							});
						}
					}
				});
			},
			/** 新增按钮操作 */
			handleAdd() {
				this.reset();
				this.open = true;
				this.title = "新增黑名单";
			},
			/** 修改按钮操作 */
			handleUpdate(row) {
				this.reset();
				this.open = true;
				this.title = "修改黑名单";
				this.form = row;
				this.form.specialFlag = row.special_flag;
			},
			// 取消按钮
			cancel() {
				this.open = false;
				this.reset();
			},
			// 表单重置
			reset() {
				this.form = {
					phone: undefined,
					specialFlag: undefined,
					special_flag: undefined,
				};
				this.resetForm("form");
			},
			/** 搜索按钮操作 */
			handleQuery() {
				this.queryParams.offset = 1;
				this.getList();
			},
			/** 重置按钮操作 */
			resetQuery() {
				this.resetForm("queryForm");
				this.handleQuery();
			},
	    /** 删除按钮操作 */
	    handleDelete(row) {
	      this.$confirm('是否确认删除数据项?', "警告", {
	          confirmButtonText: "确定",
	          cancelButtonText: "取消",
	          type: "warning"
	        }).then(function() {
	          return deleteSpecialPhone({
	          	phone: row.phone,
	          	specialFlag: row.special_flag
	          });
	        }).then(() => {
	          this.getList();
	          this.msgSuccess("删除成功");
	        })
	    }
		}
	};
</script>